﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using MvcMusicStore.Models;
using MvcMusicStore.ViewModels;
using LibraryMusicStore;
using MvcMusicStore.Infrastructure.Extensions;

namespace MvcMusicStore.Controllers
{
    [Authorize]
    public class StoreManagerController : BaseController
    {
        MusicStoreEntities storeDB = new MusicStoreEntities(); 
        
        //
        // GET: /StoreManager/
        [OwnerOnly]
        public ActionResult Index()
        {
            var albums = storeDB.Albums
                .OrderByDescending(a=>a.AlbumId).ToList();

            return View(albums);
        }

        //
        // GET: /StoreManager/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            var viewModel = new StoreManagerViewModel()
            {
                Album = new Album(),
                Genres = storeDB.Genres.ToList(),
                Artists = storeDB.Artists.ToList()
            };
            return View(viewModel);
        } 

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Album album, FormCollection collection)
        {
            if(ModelState.IsValid)
            {
                // TODO: Add insert logic here
                storeDB.Albums.InsertOnSubmit(album);
                storeDB.SubmitChanges();
                
                return RedirectToAction("Index");
            }

            //invalid - redisplay with errors
            var viewModel = new StoreManagerViewModel
            {
                Album = album,
                Genres = storeDB.Genres.ToList(),
                Artists = storeDB.Artists.ToList()
            };

            return View(viewModel);
        }
        
        //
        // GET: /StoreManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            //display edit form
            var viewModel = new StoreManagerViewModel
            {
                Album = storeDB.Albums.First(a => a.AlbumId == id),
                Genres = storeDB.Genres.ToList(),
                Artists = storeDB.Artists.ToList()
            };

            return View(viewModel);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var album = storeDB.Albums.First(a => a.AlbumId == id);

            try
            {
                // TODO: Add update logic here
                UpdateModel(album, "album");
                storeDB.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                var viewModel = new StoreManagerViewModel
                {
                    Album = album,
                    Artists = storeDB.Artists.ToList(),
                    Genres = storeDB.Genres.ToList()
                };

                return View(viewModel);
            }
        }

        //
        // GET: /StoreManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            var album = storeDB.Albums.Single(a => a.AlbumId == id);
            
            return View(album);
        }

        //
        // POST: /StoreManager/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            //try
            //{
                // TODO: Add delete logic here
                var album = storeDB.Albums.Single(a => a.AlbumId == id);

                storeDB.Carts.DeleteAllOnSubmit(album.Carts);
                storeDB.OrderDetails.DeleteAllOnSubmit(album.OrderDetails);
                storeDB.Albums.DeleteOnSubmit(album);
                storeDB.SubmitChanges();

                //return RedirectToAction("Index");
                return View("Deleted");
            //}
            //catch
            //{
                //return View("Deleted");
            //}
        }
    }
}
